#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use eframe::egui;
use egui::FontFamily;

fn main() -> Result<(), eframe::Error> {// Log to stderr (if you run with `RUST_LOG=debug`).

    let options = eframe::NativeOptions {
        initial_window_size: Some(egui::vec2(320.0, 240.0)),
        ..Default::default()
    };

    // Our application state:
    let mut name = "Arthur".to_owned();
    let mut age = 42;

    eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
        egui::CentralPanel::default().show(ctx, |ui| {
            load_global_font(ctx);
            show_menu(ui);
            ui.heading("My egui Application");
            ui.horizontal(|ui| {
               
                let name_label = ui.label("Your name: ");
                ui.text_edit_singleline(&mut name)
                    .labelled_by(name_label.id);
            });
            ui.add(egui::Slider::new(&mut age, 0..=120).text("age"));
            if ui.button("Click each year").clicked() {
                age += 1;
            }
            ui.label(format!("Hello '{name}', age {age}"));
        });
    })
}

fn show_menu(ui:&mut egui::Ui){
    use egui::{menu,Button};
    menu::bar(ui,|ui|{
        ui.menu_button("文件", |ui|{
            if ui.button("打开").clicked(){
                
            }
        });
        ui.menu_button("编辑", |ui|{
            if ui.button("编辑").clicked(){
                println!("点击了编辑");
            }
        });
    });
}


fn load_global_font(ctx: &egui::Context){
    let mut fonts = eframe::egui::FontDefinitions::default();
    // Install my own font (maybe supporting non-latin characters):
    fonts.font_data.insert("msyh".to_owned(),
                           eframe::egui::FontData::from_static(include_bytes!("C:\\Windows\\Fonts\\msyh.ttc"))); // .ttf and .otf supported
    // Put my font first (highest priority):
    fonts.families.get_mut(&eframe::egui::FontFamily::Proportional).unwrap()
        .insert(0, "msyh".to_owned());
    // Put my font as last fallback for monospace:
    fonts.families.get_mut(&eframe::egui::FontFamily::Monospace).unwrap()
        .push("msyh".to_owned());
    // let mut ctx = egui::CtxRef::default();
    ctx.set_fonts(fonts);
}
